package com.inspur.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.inspur.constant.Constant;
import com.inspur.pojo.MgrWorkHistory;
import com.inspur.pojo.MgrWorkResult;
import com.inspur.pojo.MgrWorkjc;
import com.inspur.pojo.MgrWorkkh;
import com.inspur.pojo.MgrWorkpx;
import com.inspur.pojo.MgrWorkzj;
import com.inspur.pojo.Param;
import com.inspur.pojo.RegularMeetWork;
import com.inspur.service.MgrWorkHistoryService;
import com.inspur.service.MgrWorkResultService;
import com.inspur.service.MgrWorkjcService;
import com.inspur.service.MgrWorkkhService;
import com.inspur.service.MgrWorkpxService;
import com.inspur.service.MgrWorkzjService;
import com.inspur.service.ParamService;

@Controller
public class ClientMgrOtherInfoController {
	@Autowired
	private MgrWorkResultService mgrWorkResultService;
	@Autowired
	private MgrWorkHistoryService mgrWorkHistoryService;
	@Autowired
	private ParamService paramService;
	@Autowired
	private MgrWorkzjService mgrWorkzjService;
	@Autowired	
	private MgrWorkjcService mgrWorkjcService;
	@Autowired	
	private MgrWorkkhService mgrWorkhService;
	@Autowired	
	private MgrWorkpxService mgrWorpxService;
	
	@RequestMapping(value ="/clientMgrInfoOther")
	public 	ModelAndView getAllInfoOtherByPage(int pageNum,HttpServletRequest req,Model model) {
		String mgrId = req.getParameter("mgrId");
		String cmWorkYear=req.getParameter("cmWorkYear");//1
		String startime=req.getParameter("startime");//2
		String  cmtype=req.getParameter("cmtype");//3
		 String asstime=req.getParameter("asstime");//4
		String tDate=req.getParameter("tDate");//5
		String  cmPostion=req.getParameter("cmPostion");//7
	    ModelAndView mv = new ModelAndView();
		List<MgrWorkResult> resultList = new ArrayList<MgrWorkResult>();
		List<MgrWorkHistory> workList = new ArrayList<MgrWorkHistory>();
		List<MgrWorkzj> zjList= new ArrayList<MgrWorkzj>();
		List<MgrWorkjc> jcList= new ArrayList<MgrWorkjc>();
		List<MgrWorkkh> khList= new ArrayList<MgrWorkkh>();
		List<MgrWorkpx> pxList= new ArrayList<MgrWorkpx>();
		if (pageNum == 1) {// 默认查询第一个page页的数据
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("cmWorkYear", cmWorkYear);
			resultList = mgrWorkResultService.getWorkResultList(map);
			//model.addAttribute("resultList", resultList);
         	mv.addObject("resultList", resultList);
		} else if (pageNum == 2) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("startime", startime);
			zjList = mgrWorkzjService.getWorkList(map);
			//model.addAttribute("workList", workList);
	        mv.addObject("zjList", zjList);
		} else if (pageNum == 3) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("cmtype", cmtype);
			jcList = mgrWorkjcService.getWorkList(map);
			//model.addAttribute("workList", workList);
	        mv.addObject("jcList", jcList);
		} else if (pageNum == 4) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("asstime", asstime);
			khList = mgrWorkhService.getWorkList(map);
			//model.addAttribute("workList", workList);
	        mv.addObject("khList", khList);
		}  else if (pageNum == 5) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("tDate", tDate);
			pxList = mgrWorpxService.getWorkList(map);
			//model.addAttribute("workList", workList);
	        mv.addObject("pxList", pxList);
		} 
		else if (pageNum == 7) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("mgrId", mgrId);
			map.put("cmPostion", cmPostion);
			workList = mgrWorkHistoryService.getWorkList(map);
			//model.addAttribute("workList", workList);
	       mv.addObject("workList", workList);
		}
		mv.addObject("mgrId", mgrId);
		mv.addObject("pageNum", pageNum);
		mv.setViewName("/manager/clientMgrInfoOther.jsp");
		return mv;
	}

	@ModelAttribute("mgrLevel")
	public Map<String, String> getMgrLevelList() {
		Map<String, String> levelParam = new HashMap<String, String>();
		List<Param> paramList = paramService
				.getParamList(Constant.PARAM_TYPE_MGRLEVEL);
		if (paramList != null && paramList.size() > 0) {
			for (int i = 0; i < paramList.size(); i++) {
				Param param = paramList.get(i);
				levelParam.put(param.getParamCode(), param.getParamName());
			}
		}
		return levelParam;
	}

	@RequestMapping("/modifyWork")
	public @ResponseBody List<MgrWorkHistory> getWorkHistory(String cmKey) {
		List<MgrWorkHistory> workList = mgrWorkHistoryService
				.getWorkByKey(cmKey);
		return workList;
	}
	
	@RequestMapping("/updateWorkHistory")           //工作经历修改
	public String updateWorkHistory(MgrWorkHistory hist, Model model,MultipartFile upload,HttpServletRequest req) {
		String uploadFile_name = "";
		String newFileName = "";
		try {// 原始⽂件名称
			if (upload != null) {
				uploadFile_name = upload.getOriginalFilename();
				// 新⽂件名称
				if (!"".equals(uploadFile_name))
					newFileName = UUID.randomUUID().toString()
							+ uploadFile_name.substring(uploadFile_name
									.lastIndexOf("."));
				// 上传图⽚物理路径
				String url = req.getSession().getServletContext()
						.getRealPath("/mgrWorkHist/upload");
				File uploadfile = new java.io.File(url + "/" + newFileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);
				hist.setCmAttach(newFileName);
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		// 1.获取当前时间
		Date today = new Date();
		hist.setModifyDate(today);
		mgrWorkHistoryService.updateWorkHist(hist);
		return "redirect:clientMgrInfoOther?pageNum=7&mgrId="+hist.getMgrId();
	}

	@RequestMapping("/mgrResultAdd")      //年度工作业绩增加
	public String mgrResultAdd(MgrWorkResult work, Model model) {
		// 1.获取当前时间
		Date today = new Date();
		work.setModifyDate(today);
		mgrWorkResultService.workResultAdd(work);
	
		return "redirect:clientMgrInfoOther?pageNum=1&mgrId="+work.getMgrId();
	}
	
	@RequestMapping("/workzjAdd")          //证件列表增加
	public String workzjAdd(MgrWorkzj MgrWorkzj) {
		// 1.获取当前时间
		Date modificationtime = new Date();
		MgrWorkzj.setModificationtime(modificationtime);
		mgrWorkzjService.workzjAdd(MgrWorkzj);
	
		return "redirect:clientMgrInfoOther?pageNum=2&mgrId="+MgrWorkzj.getMgrId();
	}
	
	@RequestMapping("/workjcAdd")          //奖惩记录增加
	public String workjcAdd(MgrWorkjc MgrWorkjc) {
		// 1.获取当前时间
		Date whData = new Date();
		MgrWorkjc.setWhData(whData);
		mgrWorkjcService.workjcAdd(MgrWorkjc);
	
		return "redirect:clientMgrInfoOther?pageNum=3";
	}
	

	@RequestMapping("/deleteresult")           //年度工作业绩删除
	public String deleteresult(String cmkeys){
		String cmkey[] = cmkeys.split(",");
		int meet_cmkeys[] = new int[cmkey.length];
		for(int i=0;i<cmkey.length;i++){
			meet_cmkeys[i] = Integer.parseInt(cmkey[i]);
		}
		mgrWorkResultService.deleteresult(meet_cmkeys);
		
		//删除数据后要重新查询一次
		return "redirect:clientMgrInfoOther?pageNum=1";
	}
	@RequestMapping("/deletezj")                 //证件列表删除
	public String deletezj(String cmkeys){
		String cmkey[] = cmkeys.split(",");
		int meet_cmkeys[] = new int[cmkey.length];
		for(int i=0;i<cmkey.length;i++){
			meet_cmkeys[i] = Integer.parseInt(cmkey[i]);
		}
		mgrWorkzjService.deletezj(meet_cmkeys);
		
		//删除数据后要重新查询一次
		return "redirect:clientMgrInfoOther?pageNum=2";
	}
	@RequestMapping("/deletejc")                 //奖惩记录删除
	public String deletejc(String cmkeys){
		String cmkey[] = cmkeys.split(",");
		int meet_cmkeys[] = new int[cmkey.length];
		for(int i=0;i<cmkey.length;i++){
			meet_cmkeys[i] = Integer.parseInt(cmkey[i]);
		}
		mgrWorkjcService.deletejc(meet_cmkeys);
		
		//删除数据后要重新查询一次
		return "redirect:clientMgrInfoOther?pageNum=3";
	}
	
	@RequestMapping("/regularresultUpdate")                    //年度工作业绩修改
	public String regularresultUpdate(MgrWorkResult mgrWorkResult) {
		
		// 1.获取当前时间
		Date today = new Date();
		mgrWorkResult.setModifyDate(today);
		mgrWorkResultService.regularresultUpdate(mgrWorkResult);
	
		return "redirect:clientMgrInfoOther?pageNum=1";
	}
	
	@RequestMapping("/modifyresult")                       //年度工作业绩修改
	public @ResponseBody List<MgrWorkResult> modifyresult(String cmkey,Model model){
		  int meet_key = Integer.parseInt(cmkey);
		
		List<MgrWorkResult> resultList = mgrWorkResultService.getresultbycmkey(meet_key);
	     model.addAttribute("resultList", resultList);
	  return resultList;
	}
	
	@RequestMapping("/modifyzj")                           //证件修改
	public @ResponseBody List<MgrWorkzj> modifyzj(String cmkey,Model model){
		  int meet_key = Integer.parseInt(cmkey);
		
		List<MgrWorkzj> zjList = mgrWorkzjService.getzjbycmkey(meet_key);
	     model.addAttribute("zjList", zjList);
	  return zjList;
	}
	
	@RequestMapping("/zjUpdate")                         //证件修改
	public String zjUpdate(MgrWorkzj mgrWorkzj) {
		
		// 1.获取当前时间
		Date modificationtime = new Date();
		mgrWorkzj.setModificationtime(modificationtime);
		mgrWorkzjService.zjUpdate(mgrWorkzj);
	
		return "redirect:clientMgrInfoOther?pageNum=2";
	}
	
	
	
	@RequestMapping("/modifyjc")                           //奖惩修改
	public @ResponseBody List<MgrWorkjc> modifyjc(String cmkey,Model model){
		  int meet_key = Integer.parseInt(cmkey);
		
		List<MgrWorkjc> jcList = mgrWorkjcService.getjcbycmkey(meet_key);
	     model.addAttribute("jcList", jcList);
	  return jcList;
	}
	
	@RequestMapping("/jcUpdate")                         //证件修改
	public String jcUpdate(MgrWorkjc mgrWorkjc) {
		
		// 1.获取当前时间
		Date whData = new Date();
		mgrWorkjc.setWhData(whData);
		mgrWorkjcService.jcUpdate(mgrWorkjc);
	
		return "redirect:clientMgrInfoOther?pageNum=3";
	}
	
	
	
	
	
}
